package com.ithao.controller;

import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.entity.Result;
import com.ithao.pojo.Permission;
import com.ithao.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/4/3
 * @description :
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/findAll")
    public Result findAll() {
        List<Permission> permissionList = permissionService.findAll();
        return new Result(true, "查询成功", permissionList);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        permissionService.delete(id);
        return new Result(true,"删除成功");
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean == null) {
            queryPageBean = new QueryPageBean();
            queryPageBean.setCurrentPage(1);
            queryPageBean.setPageSize(10);
        }
        log.info("[权限 分页] queryPageBean:{}",queryPageBean);
       PageResult pageResult = permissionService.findPage(queryPageBean);
       return new Result(true,"查找成功",pageResult);
    }
    @PostMapping("add")
    public Result add(@RequestBody Permission permission) {
        log.info("[权限-新增]data:{}", permission);

        permissionService.add(permission);

        return new Result(true, "新增成功");
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限-编辑]data:{}", permission);

        permissionService.edit(permission);
        return new Result(true, "更新成功");

    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.info("[权限-根据Id查询]data:{}",id);
        Permission permission = permissionService.findById(id);
        return new Result(true,"查找成功",permission);
    }
}
